<template>
  <Nav>
    {{ $t('home.my_info') }}
  </Nav>
  <div class="page-home-info">
    <div class="content">
      <div class="avatar" @click="avatar.click()">
        <img v-if="user.avatar" :src="user.avatar">
        <img v-else src="@/assets/home/my/avatar.png">
        <input ref="avatar" @change="avatarUpload" type="file" capture="camera" accept="image/*" style="display:none">
      </div>
      
      <div class="box">
        <md-field>
          <md-input-item v-model="user.name" :title="$t('home.info.name')"></md-input-item>
          <md-input-item v-model="user.phone" type="phone" :maxlength="11" :title="$t('home.info.phone')"></md-input-item>
          <div style="padding-top: 20px">
            <md-button type="primary" :loading="loading" @click="update" round>{{ $t('confirm') }}</md-button>
          </div>
        </md-field>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { userData, userUpdate } from '@/model/user'
import { upload } from '@/api/base'
import { toast } from '@/style'
import { t } from '@/lang'
import Nav from '@/components/nav.vue'

const user = ref(Object.assign({}, userData.value))

const avatar = ref(null)
const avatarUpload = () => {
  upload(avatar.value.files[0]).then(res => {
    user.value.avatar = res.path
  }).catch(err => {
    toast(err.message)
  })
} 

const loading = ref(false)
const update = () => {
  loading.value = true
  userUpdate(user.value).then(() => {
    toast(t('ok'))
  }).catch(err => {
    toast(err.message)
  }).finally(() => {
    loading.value = false
  })
}

</script>

<style lang="scss" scoped>
  .page-home-info{
    min-height: 100%;
    background: url('@/assets/page.jpg') center center no-repeat;
    background-size: cover;

    .content{
      position: relative;
      padding: 200px 30px 30px;
      overflow: hidden;
    }

    .padding{
      padding: 30px;
    }

    .box{
      overflow: hidden;
      background: #fff;
      border-radius: 15px;
      box-shadow: 0 10px 20px rgba(0,0,0, .1);
    }

    .avatar{
      width: 100px;
      height: 100px;
      margin: 0 auto 40px;
      border-radius: 10px;  
      box-shadow: 0 10px 10px rgba(0,0,0, .1);
      overflow: hidden;

      img{
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }
</style>